package me.lwn.auth.security.web.authentication;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.Collections;

public class SmsAuthenticationToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = -6067207202119450764L;

    private final String mobile;

    private final String code;

    public SmsAuthenticationToken(String mobile, String code) {
        this(mobile, code, Collections.emptyList());
    }

    public SmsAuthenticationToken(String mobile, String code, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        Assert.notNull(mobile, "mobile cannot be null");
        Assert.notNull(code, "code cannot be null");
        this.mobile = mobile;
        this.code = code;
        setAuthenticated(false);
    }

    @Override
    public Object getPrincipal() {
        return this.mobile;
    }

    @Override
    public Object getCredentials() {
        return this.code;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        Assert.isTrue(!isAuthenticated,
                "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        super.setAuthenticated(false);
    }

    public String getMobile() {
        return mobile;
    }

    public String getCode() {
        return code;
    }
}
